package com.qf.utils;

import com.qf.entity.Car;
import com.qf.entity.Meeting;
import com.qf.entity.ReserveMeet;
import com.qf.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


public class ExcelUtils {
        //1、创建一个excle文件
         static HSSFWorkbook hssfWorkbook;
         //2、创建文件中的sheet表，参数是sheet名称
         static HSSFSheet sheet;
         //创建名，创建表的第一行
        public static void createFirstRow(String sheetName,Integer type){
            hssfWorkbook=new HSSFWorkbook();
            sheet=hssfWorkbook.createSheet(sheetName);
            //创建第一行的字段名
            HSSFRow row = sheet.createRow(0);

            if (type==1){
                String[] heads={"车型","车牌号","颜色","备注"};
                for (int i = 0; i < heads.length; i++) {
                    //创建列
                    HSSFCell cell = row.createCell(i);
                    //每一列的值
                    cell.setCellValue(heads[i]);
                }
            }else if (type==2){
                String[] heads={"会议室名称","会议室描述"};
                for (int i = 0; i < heads.length; i++) {
                    //创建列
                    HSSFCell cell = row.createCell(i);
                    //每一列的值
                    cell.setCellValue(heads[i]);
                }
            }else if (type==3){
                String[] heads={"会议室名称","会议起始时间","会议结束时间","会议室用途","批准人","预定人","预定时间"};
                for (int i = 0; i < heads.length; i++) {
                    //创建列
                    HSSFCell cell = row.createCell(i);
                    //每一列的值
                    cell.setCellValue(heads[i]);
                }
            }
        }

        //3、循环创建其他行的值 导出数据
    public static void createSheetValueCar(List<Car> objs){

            //向这个表中的其他行赋值
            for (int i = 0; i < objs.size(); i++) {
                //创建第二行及以后
                HSSFRow row = sheet.createRow(i + 1);
                //当前行的值
                Car car = (Car) objs.get(i);
                //向第二行及以后的每一列中赋值

                //第二行第一列
                HSSFCell cell1 = row.createCell(0);
                cell1.setCellValue(car.getVehicleType());
                //第二行第二列
                HSSFCell cell2 = row.createCell(1);
                cell2.setCellValue(car.getNumCard());
                //第二行第三列
                HSSFCell cell3 = row.createCell(2);
                cell3.setCellValue(car.getVehicleColor());
                //第二行第四列
                HSSFCell cell4 = row.createCell(3);
                cell4.setCellValue(car.getContent());
        }
    }

    //3、循环创建其他行的值 导出数据
    public static void createSheetValueMeet(List<Meeting> objs){

        //向这个表中的其他行赋值
        for (int i = 0; i < objs.size(); i++) {
            //创建第二行及以后
            HSSFRow row = sheet.createRow(i + 1);
            //当前行的值
            Meeting meeting = (Meeting) objs.get(i);
            //向第二行及以后的每一列中赋值
            //第二行第一列
            HSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(meeting.getMeetName());
            //第二行第二列
            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(meeting.getMeetDesc());
        }
    }

    //3、循环创建其他行的值 导出数据
    public static void createSheetValueReserveMeet(List<ReserveMeet> objs){

        //向这个表中的其他行赋值
        for (int i = 0; i < objs.size(); i++) {
            //创建第二行及以后
            HSSFRow row = sheet.createRow(i + 1);
            //当前行的值
            ReserveMeet reserveMeet = (ReserveMeet) objs.get(i);
            //向第二行及以后的每一列中赋值

            //第二行第一列
            HSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(reserveMeet.getMeetId());
            //第二行第二列
            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(reserveMeet.getStartTime());
            //第二行第三列
            HSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(reserveMeet.getEndTime());
            //第二行第四列
            HSSFCell cell4 = row.createCell(3);
            cell4.setCellValue(reserveMeet.getMeetTypeId());
            //第二行第五列
            HSSFCell cell5 = row.createCell(4);
            cell5.setCellValue(reserveMeet.getUserId());
            //第二行第六列
            HSSFCell cell6 = row.createCell(5);
            cell6.setCellValue(reserveMeet.getRealName());
            //第二行第七列
            HSSFCell cell7 = row.createCell(6);
            cell7.setCellValue(reserveMeet.getApplyTime());

        }
    }

    //excle输出 字节输出
    public static void export(OutputStream outputStream) throws IOException {

        //以网格的格式输出
        sheet.setGridsPrinted(true);
        //创建一个文件
        //以字节流响应给用户
        hssfWorkbook.write(outputStream);
        outputStream.flush();
    }


}
